<?php
/**
 * Created by PhpStorm.
 * User: taj
 * Date: 14-10-4
 * Time: 下午8:25
 */
class Task_Updatedoskuname extends Minion_Task
{
    /**
     * Generates a help list for all tasks
     *
     * @return null
     */


    protected $_options = array(
        'start'=>NULL,
        'end'=>NULL,
        'notice'=>NULL,
    );
    //php f:\wamp\www\newerp\index.php --task=delivery --start="2014-03-27 " --end="2014-03-27 23:59:28"

    protected function _execute(array $params)
    {
        set_time_limit(0);
        ini_set('memory_limit','500M');
        $start='';
        $end='';
        echo '更新出库单的中文名称开始时间是'.date('Y-m-d H:i:s');
        if(isset($params['notice'])){
            $start=date('Y-m-d H:i:s',strtotime("-1 days"));
            $end=date('Y-m-d H:i:s');
           // $start='2014-09-22 15:30:01';
           // $end='2014-09-22 15:40:01';
        }elseif(!isset($params['start'])||!isset($params['end'])){
            $start=date('Y-m-d H:i:s',strtotime("-40 minutes"));
            $end=date('Y-m-d H:i:s');
        }else {
            $start=$params['start'];
            $end=$params['end'];
        }
        $do_time = array();
        $dos_sql = "SELECT sku FROM `deliveryorders`
        WHERE `type`=10 AND sku!='' AND sku_name='' AND `delivery_date`>='".$start."' AND delivery_date<='".$end."' group by sku";
        $dos = DB::query(Database::SELECT,$dos_sql)->execute('newerp')->as_array();
        if(!empty($dos)){
            $sku_arr = "('";

            foreach($dos as $do){
                $sku_arr .=$do['sku']."','";
            }

            $sku_arr = rtrim($sku_arr,",'");
            $sku_arr = $sku_arr."')";
            //获取sku的中文名称
            $results = skusystemapi::getMoreSkuCnname($sku_arr);
            $results = json_decode($results);
            $sql_update_cnname ='';
            if(!empty($results)){
            foreach($results as $re){

                $sql_update_cnname.=" WHEN '".$re->sku."' THEN '".$re->name."'";

            }
            $sql_update="UPDATE deliveryorders SET sku_name=(CASE sku ".$sql_update_cnname." END)
             WHERE `type`=10 AND `delivery_date`>='".$start."' AND delivery_date<='".$end."' AND sku in".$sku_arr;
            DB::query(Database::UPDATE,$sql_update)->execute('newerp');
        }}
        echo '更新出库单的中文名称结束时间是'.date('Y-m-d H:i:s')."\n";

    }
}